package cc.ufinity.networkdiagnose.manager;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Iterator;
import java.util.List;

import android.content.Context;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiConfiguration.Protocol;
import android.net.wifi.WifiManager;
import android.net.wifi.WifiConfiguration.AuthAlgorithm;
import android.net.wifi.WifiConfiguration.GroupCipher;
import android.net.wifi.WifiConfiguration.KeyMgmt;
import android.net.wifi.WifiConfiguration.PairwiseCipher;
import android.os.Environment;
import android.util.Log;
import android.widget.Toast;

public class WifiEAPManager {
		private static final String INT_PRIVATE_KEY = "private_key";      
	    private static final String INT_PHASE2 = "phase2";     
	    private static final String INT_PASSWORD = "password";     
	    private static final String INT_IDENTITY = "identity";      
	    private static final String INT_EAP = "eap";      
	    private static final String INT_CLIENT_CERT = "client_cert";     
	    private static final String INT_CA_CERT = "ca_cert";      
	    private static final String INT_ANONYMOUS_IDENTITY = "anonymous_identity";     
	    final String INT_ENTERPRISEFIELD_NAME = "android.net.wifi.WifiConfiguration$EnterpriseField";  
	    Context context;
	    public WifiEAPManager(Context context){
	    	this.context = context;
	    	BufferedWriter out = null;         
	 	    try{              
	 	    	File root = Environment.getExternalStorageDirectory();      
	 	    	if (root.canWrite())              
	 	    	{                 
	 	    		File gpxfile = new File(root, "ReadConfigLog.txt");                  
	 	    		FileWriter gpxwriter = new FileWriter(gpxfile);                 
	 	    		out = new BufferedWriter(gpxwriter);                 
	 	    		out.write("Hello world");                 
	 	    		//out.close();            
	 	    	}else {
	 	    		Toast toast = Toast.makeText(context, "无权限操作系统文件，修改配置参数失败", 3000); 
		 	    	toast.show();      
				}         
 	    	} catch (IOException e) {              
 	    		Toast toast = Toast.makeText(context, "Problem reading SD CARD", 3000);            
 	    		Toast toast2 = Toast.makeText(context, "Please take logs using Logcat", 3000);     
 	    		Log.e("<<<<<<<<<<WifiPreference>>>>>>>>>>>>", "Could not write file " + e.getMessage()); 
	       }  
	  } 
	  public void saveEapConfig(String passString, String userName,String anonIdent)     
	  {           
		  /********************************Configuration Strings****************************************************/       
		  final String ENTERPRISE_EAP = "PEAP";           
		  final String ENTERPRISE_CLIENT_CERT = "";        
		  final String ENTERPRISE_PRIV_KEY = "";   
		  //CertificateName = Name given to the certificate while installing it           
		  /*Optional Params- My wireless Doesn't use these*/          
		  final String ENTERPRISE_PHASE2 = "MSCHAPV2";         
		  final String ENTERPRISE_ANON_IDENT = anonIdent;  
		  final String ENTERPRISE_CA_CERT = "";          
		  /********************************Configuration Strings****************************************************/
		  /*Create a WifiConfig*/          
		  WifiConfiguration selectedConfig = new WifiConfiguration();     
		  /*AP Name*/          
		  selectedConfig.SSID = "\"CMCC-AUTO\"";    
		  /*Priority*/         
		  selectedConfig.priority = 40;           
		  /*Enable Hidden SSID*/      
		  selectedConfig.hiddenSSID = true;     
		  /*Key Mgmnt*/         
		  selectedConfig.allowedKeyManagement.clear();   
		  selectedConfig.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.IEEE8021X);    
		  selectedConfig.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_EAP);   
		  /*Group Ciphers*/        
		  selectedConfig.allowedGroupCiphers.clear();    
		  selectedConfig.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);       
		  selectedConfig.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);    
		  selectedConfig.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP104);      
		  selectedConfig.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);     
		  /*Pairwise ciphers*/         
		  selectedConfig.allowedPairwiseCiphers.clear();    
		  selectedConfig.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);    
		  selectedConfig.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);   
		  /*Protocols*/        
		  selectedConfig.allowedProtocols.clear();   
		  selectedConfig.allowedProtocols.set(WifiConfiguration.Protocol.RSN);       
		  selectedConfig.allowedProtocols.set(WifiConfiguration.Protocol.WPA);           
		  // Enterprise Settings         
		  // Reflection magic here too, need access to non-public APIs    
		  try {             
			  // Let the magic start            
			  Class[] wcClasses = WifiConfiguration.class.getClasses();      
			  // null for overzealous java compiler            
			  Class wcEnterpriseField = null;               
			  for (Class wcClass : wcClasses)               
				  if (wcClass.getName().equals(INT_ENTERPRISEFIELD_NAME))   
				  {                    
					  wcEnterpriseField = wcClass;          
					  break;                 
				  }            
			  boolean noEnterpriseFieldType = false;       
			  if(wcEnterpriseField == null)                
				  noEnterpriseFieldType = true;
			  // Cupcake/Donut access enterprise settings directly      
			  Field wcefAnonymousId = null, wcefCaCert = null, wcefClientCert = null, wcefEap = null, wcefIdentity = null, wcefPassword = null, wcefPhase2 = null, wcefPrivateKey = null;              
			  Field[] wcefFields = WifiConfiguration.class.getFields();           
			  // Dispatching Field vars            
			  for (Field wcefField : wcefFields)            
			  {                 
				  if (wcefField.getName().equals(INT_ANONYMOUS_IDENTITY))      
					  wcefAnonymousId = wcefField;                 
				  else if (wcefField.getName().equals(INT_CA_CERT))     
					  wcefCaCert = wcefField;                 
				  else if (wcefField.getName().equals(INT_CLIENT_CERT))     
					  wcefClientCert = wcefField;              
				  else if (wcefField.getName().equals(INT_EAP))          
					  wcefEap = wcefField;                
				  else if (wcefField.getName().equals(INT_IDENTITY))      
					  wcefIdentity = wcefField;                
				  else if (wcefField.getName().equals(INT_PASSWORD))     
					  wcefPassword = wcefField;               
				  else if (wcefField.getName().equals(INT_PHASE2))      
					  wcefPhase2 = wcefField;                
				  else if (wcefField.getName().equals(INT_PRIVATE_KEY))      
					  wcefPrivateKey = wcefField;           
				  }                 
			     Method wcefSetValue = null;      
			  	 if(!noEnterpriseFieldType){              
			  		 for(Method m: wcEnterpriseField.getMethods())
			  			 //System.out.println(m.getName());               
			  			 if(m.getName().trim().equals("setValue"))          
			  				 wcefSetValue = m;             
			  	 }                  
			  	 /*EAP Method*/          
			  	 if(!noEnterpriseFieldType)       
			  		 wcefSetValue.invoke(wcefEap.get(selectedConfig), ENTERPRISE_EAP);       
			  	 /*EAP Phase 2 Authentication*/            
			  	 if(!noEnterpriseFieldType)                 
			  		 wcefSetValue.invoke(wcefPhase2.get(selectedConfig), ENTERPRISE_PHASE2);       
			  	 /*EAP Anonymous Identity*/       
			  	 if(!noEnterpriseFieldType)                 
			  		 wcefSetValue.invoke(wcefAnonymousId.get(selectedConfig), ENTERPRISE_ANON_IDENT);      
			  	 /*EAP CA Certificate*/             
			  	 if(!noEnterpriseFieldType)             
			  		 wcefSetValue.invoke(wcefCaCert.get(selectedConfig), ENTERPRISE_CA_CERT);        
			  	 /*EAP Private key*/              
			  	 if(!noEnterpriseFieldType)              
			  		 wcefSetValue.invoke(wcefPrivateKey.get(selectedConfig), ENTERPRISE_PRIV_KEY);              
			  	 /*EAP Identity*/            
			  	 if(!noEnterpriseFieldType)          
			  		 wcefSetValue.invoke(wcefIdentity.get(selectedConfig), userName);       
			  	 /*EAP Password*/             
			  	 if(!noEnterpriseFieldType)           
			  		 wcefSetValue.invoke(wcefPassword.get(selectedConfig), passString);       
			  	 /*EAp Client certificate*/             
			  	 if(!noEnterpriseFieldType)               
			  		 wcefSetValue.invoke(wcefClientCert.get(selectedConfig), ENTERPRISE_CLIENT_CERT);      
			  	 // Adhoc for CM6            
			  	 // if non-CM6 fails gracefully thanks to nested try-catch     
			  	 try{            
			  		 Field wcAdhoc = WifiConfiguration.class.getField("adhocSSID");      
			  		 Field wcAdhocFreq = WifiConfiguration.class.getField("frequency");       
			  		 //wcAdhoc.setBoolean(selectedConfig, prefs.getBoolean(PREF_ADHOC,false));            
			  		 wcAdhoc.setBoolean(selectedConfig, false);        
			  		 int freq = 2462;   
			  		 // default to channel 11      
			  		 //int freq = Integer.parseInt(prefs.getString(PREF_ADHOC_FREQUENCY, "2462"));    
			  		 // default to channel 11          
			  		 wcAdhocFreq.setInt(selectedConfig, freq);     
		  		 } catch (Exception e)           
		  		 {              
		  			 e.printStackTrace();     
		  		 }     
		  } catch (Exception e) 
		  {            
	  		 // TODO Auto-generated catch block    
	  		 // FIXME As above, what should I do here?    
	  		 e.printStackTrace();       
		  }           
		  WifiManager wifiManag = (WifiManager)context.getSystemService(Context.WIFI_SERVICE);   
		  boolean res1 = wifiManag.setWifiEnabled(true);      
		  int res = wifiManag.addNetwork(selectedConfig);        
		  Log.d("WifiPreference", "add Network returned " + res );      
		  boolean b = wifiManag.enableNetwork(selectedConfig.networkId, false); 
		  Log.d("WifiPreference", "enableNetwork returned " + b );        
		  boolean c = wifiManag.saveConfiguration();      
		  Log.d("WifiPreference", "Save configuration returned " + c );        
		  boolean d = wifiManag.enableNetwork(res, true);        
		  Log.d("WifiPreference", "enableNetwork returned " + d );   
		}  
 
	  public void readEapConfig(BufferedWriter out)      
	  {         
		  /*Get the WifiService */                  
		  WifiManager wifi = (WifiManager)context.getSystemService(Context.WIFI_SERVICE);          
		  /*Get All WIfi configurations*/          
		  List<WifiConfiguration> configList = wifi.getConfiguredNetworks();          
		  /*Now we need to search appropriate configuration i.e. with name SSID_Name*/          
		  for(int i = 0;i<configList.size();i++)          
		  {              
			  if(configList.get(i).SSID.contentEquals("\"CMCC-AUTO\""))              
			  {                 
				  /*We found the appropriate config now read all config details*/                  
				  Iterator<WifiConfiguration> iter =  configList.iterator();                  
				  WifiConfiguration config = configList.get(i);                   
				  /*I dont think these fields have anything to do with EAP config but still will print these to be on safe side*/   
				  try {                 
					  Log.d("<<<<<<<<<<WifiPreference>>>>>>>>>>>>", "[SSID]" + config.SSID);                  
					  out.write("<<<<<<<<<<WifiPreference>>>>>>>>>>>>" + "[SSID]" + config.SSID);                
					  Log.d("<<<<<<<<<<WifiPreference>>>>>>>>>>>>", "[BSSID]" + config.BSSID);          
					  out.write("<<<<<<<<<<WifiPreference>>>>>>>>>>>>" +"[BSSID]" + config.BSSID);          
					  Log.d("<<<<<<<<<<WifiPreference>>>>>>>>>>>>", "[HIDDEN SSID]" + config.hiddenSSID);    
					  out.write("<<<<<<<<<<WifiPreference>>>>>>>>>>>>" + "[HIDDEN SSID]" + config.hiddenSSID);  
					  Log.d("<<<<<<<<<<WifiPreference>>>>>>>>>>>>", "[PASSWORD]" + config.preSharedKey);          
					  out.write("<<<<<<<<<<WifiPreference>>>>>>>>>>>>"+ "[PASSWORD]" + config.preSharedKey);      
					  Log.d("<<<<<<<<<<WifiPreference>>>>>>>>>>>>", "[ALLOWED ALGORITHMS]");               
					  out.write("<<<<<<<<<<WifiPreference>>>>>>>>>>>>"+ "[ALLOWED ALGORITHMS]");              
					  Log.d("<<<<<<<<<<WifiPreference>>>>>>>>>>>>", "[LEAP]" + config.allowedAuthAlgorithms.get(AuthAlgorithm.LEAP));         
					  out.write("<<<<<<<<<<WifiPreference>>>>>>>>>>>>" + "[LEAP]" + config.allowedAuthAlgorithms.get(AuthAlgorithm.LEAP));              
					  Log.d("<<<<<<<<<<WifiPreference>>>>>>>>>>>>", "[OPEN]" + config.allowedAuthAlgorithms.get(AuthAlgorithm.OPEN));             
					  out.write("<<<<<<<<<<WifiPreference>>>>>>>>>>>>" + "[OPEN]" + config.allowedAuthAlgorithms.get(AuthAlgorithm.OPEN));               
					  Log.d("<<<<<<<<<<WifiPreference>>>>>>>>>>>>", "[SHARED]" + config.allowedAuthAlgorithms.get(AuthAlgorithm.SHARED));                
					  out.write("<<<<<<<<<<WifiPreference>>>>>>>>>>>>" + "[SHARED]" + config.allowedAuthAlgorithms.get(AuthAlgorithm.SHARED));   
					  Log.d("<<<<<<<<<<WifiPreference>>>>>>>>>>>>", "[GROUP CIPHERS]");                 
					  out.write("<<<<<<<<<<WifiPreference>>>>>>>>>>>>" + "[GROUP CIPHERS]");                
					  Log.d("<<<<<<<<<<WifiPreference>>>>>>>>>>>>", "[CCMP]" + config.allowedGroupCiphers.get(GroupCipher.CCMP));        
					  out.write("<<<<<<<<<<WifiPreference>>>>>>>>>>>>" + "[CCMP]" + config.allowedGroupCiphers.get(GroupCipher.CCMP));    
					  Log.d("<<<<<<<<<<WifiPreference>>>>>>>>>>>>" , "[TKIP]" + config.allowedGroupCiphers.get(GroupCipher.TKIP));           
					  out.write("<<<<<<<<<<WifiPreference>>>>>>>>>>>>"+ "[TKIP]" + config.allowedGroupCiphers.get(GroupCipher.TKIP));           
					  Log.d("<<<<<<<<<<WifiPreference>>>>>>>>>>>>", "[WEP104]" + config.allowedGroupCiphers.get(GroupCipher.WEP104));        
					  out.write("<<<<<<<<<<WifiPreference>>>>>>>>>>>>" + "[WEP104]" + config.allowedGroupCiphers.get(GroupCipher.WEP104));  
					  Log.d("<<<<<<<<<<WifiPreference>>>>>>>>>>>>", "[WEP40]" + config.allowedGroupCiphers.get(GroupCipher.WEP40));          
					  out.write("<<<<<<<<<<WifiPreference>>>>>>>>>>>>" + "[WEP40]" + config.allowedGroupCiphers.get(GroupCipher.WEP40));     
					  Log.d("<<<<<<<<<<WifiPreference>>>>>>>>>>>>", "[KEYMGMT]");                
					  out.write("<<<<<<<<<<WifiPreference>>>>>>>>>>>>" + "[KEYMGMT]");            
					  Log.d("<<<<<<<<<<WifiPreference>>>>>>>>>>>>", "[IEEE8021X]" + config.allowedKeyManagement.get(KeyMgmt.IEEE8021X));    
					  out.write("<<<<<<<<<<WifiPreference>>>>>>>>>>>>"+ "[IEEE8021X]" + config.allowedKeyManagement.get(KeyMgmt.IEEE8021X)); 
					  Log.d("<<<<<<<<<<WifiPreference>>>>>>>>>>>>", "[NONE]" + config.allowedKeyManagement.get(KeyMgmt.NONE));               
					  out.write("<<<<<<<<<<WifiPreference>>>>>>>>>>>>" + "[NONE]" + config.allowedKeyManagement.get(KeyMgmt.NONE));          
					  Log.d("<<<<<<<<<<WifiPreference>>>>>>>>>>>>", "[WPA_EAP]" + config.allowedKeyManagement.get(KeyMgmt.WPA_EAP));         
					  out.write("<<<<<<<<<<WifiPreference>>>>>>>>>>>>" + "[WPA_EAP]" + config.allowedKeyManagement.get(KeyMgmt.WPA_EAP));     
					  Log.d("<<<<<<<<<<WifiPreference>>>>>>>>>>>>", "[WPA_PSK]" + config.allowedKeyManagement.get(KeyMgmt.WPA_PSK));   
					  out.write("<<<<<<<<<<WifiPreference>>>>>>>>>>>>" + "[WPA_PSK]" + config.allowedKeyManagement.get(KeyMgmt.WPA_PSK));    
					  Log.d("<<<<<<<<<<WifiPreference>>>>>>>>>>>>", "[PairWiseCipher]");                
					  out.write("<<<<<<<<<<WifiPreference>>>>>>>>>>>>" + "[PairWiseCipher]");              
					  Log.d("<<<<<<<<<<WifiPreference>>>>>>>>>>>>", "[CCMP]" + config.allowedPairwiseCiphers.get(PairwiseCipher.CCMP));         
					  out.write("<<<<<<<<<<WifiPreference>>>>>>>>>>>>" + "[CCMP]" + config.allowedPairwiseCiphers.get(PairwiseCipher.CCMP));    
					  Log.d("<<<<<<<<<<WifiPreference>>>>>>>>>>>>", "[NONE]" + config.allowedPairwiseCiphers.get(PairwiseCipher.NONE));       
					  out.write("<<<<<<<<<<WifiPreference>>>>>>>>>>>>" + "[NONE]" + config.allowedPairwiseCiphers.get(PairwiseCipher.NONE));    
					  Log.d("<<<<<<<<<<WifiPreference>>>>>>>>>>>>", "[TKIP]" + config.allowedPairwiseCiphers.get(PairwiseCipher.TKIP));      
					  out.write("<<<<<<<<<<WifiPreference>>>>>>>>>>>>" + "[TKIP]" + config.allowedPairwiseCiphers.get(PairwiseCipher.TKIP));   
					  Log.d("<<<<<<<<<<WifiPreference>>>>>>>>>>>>", "[Protocols]");               
					  out.write("<<<<<<<<<<WifiPreference>>>>>>>>>>>>" + "[Protocols]");               
					  Log.d("<<<<<<<<<<WifiPreference>>>>>>>>>>>>", "[RSN]" + config.allowedProtocols.get(Protocol.RSN));        
					  out.write("<<<<<<<<<<WifiPreference>>>>>>>>>>>>" + "[RSN]" + config.allowedProtocols.get(Protocol.RSN));      
					  Log.d("<<<<<<<<<<WifiPreference>>>>>>>>>>>>", "[WPA]" + config.allowedProtocols.get(Protocol.WPA));            
					  out.write("<<<<<<<<<<WifiPreference>>>>>>>>>>>>" + "[WPA]" + config.allowedProtocols.get(Protocol.WPA));           
					  Log.d("<<<<<<<<<<WifiPreference>>>>>>>>>>>>", "[PRE_SHARED_KEY]" + config.preSharedKey);             
					  out.write("<<<<<<<<<<WifiPreference>>>>>>>>>>>>" + "[PRE_SHARED_KEY]" + config.preSharedKey);              
					  Log.d("<<<<<<<<<<WifiPreference>>>>>>>>>>>>", "[WEP Key Strings]");              
					  out.write("<<<<<<<<<<WifiPreference>>>>>>>>>>>>" + "[WEP Key Strings]");              
					  String[] wepKeys = config.wepKeys;                 
					  Log.d("<<<<<<<<<<WifiPreference>>>>>>>>>>>>", "[WEP KEY 0]" + wepKeys[0]);                  
					  out.write("<<<<<<<<<<WifiPreference>>>>>>>>>>>>" + "[WEP KEY 0]" + wepKeys[0]);               
					  Log.d("<<<<<<<<<<WifiPreference>>>>>>>>>>>>", "[WEP KEY 1]" + wepKeys[1]);              
					  out.write("<<<<<<<<<<WifiPreference>>>>>>>>>>>>" + "[WEP KEY 1]" + wepKeys[1]);                
					  Log.d("<<<<<<<<<<WifiPreference>>>>>>>>>>>>", "[WEP KEY 2]" + wepKeys[2]);             
					  out.write("<<<<<<<<<<WifiPreference>>>>>>>>>>>>" + "[WEP KEY 2]" + wepKeys[2]);               
					  Log.d("<<<<<<<<<<WifiPreference>>>>>>>>>>>>", "[WEP KEY 3]" + wepKeys[3]);                 
					  out.write("<<<<<<<<<<WifiPreference>>>>>>>>>>>>" + "[WEP KEY 3]" + wepKeys[3]);            
				  }catch(IOException e){                 
						  Toast toast1 = Toast.makeText(context, "Failed to write Logs to ReadConfigLog.txt", 3000);                 
						  Toast toast2 = Toast.makeText(context, "Please take logs using Logcat", 5000);                    
						  Log.e("<<<<<<<<<<WifiPreference>>>>>>>>>>>>", "Could not write to ReadConfigLog.txt" + e.getMessage());       
				 }                  /*reflection magic*/                  /*These are the fields we are really interested in*/              
				 try                  
				  {      
					 // Let the magic start           
					 Class[] wcClasses = WifiConfiguration.class.getClasses();            
					 // null for overzealous java compiler                    
					 Class wcEnterpriseField = null;                     
					 for (Class wcClass : wcClasses)                      
						 if (wcClass.getName().equals(INT_ENTERPRISEFIELD_NAME))      
						 {                             
							 wcEnterpriseField = wcClass;    
							 break;                        
						 }                   
					 boolean noEnterpriseFieldType = false;              
					 if(wcEnterpriseField == null)                    
						 noEnterpriseFieldType = true; 
					 // Cupcake/Donut access enterprise settings directly        
					 Field wcefAnonymousId = null, wcefCaCert = null, wcefClientCert = null, wcefEap = null, wcefIdentity = null, wcefPassword = null, wcefPhase2 = null, wcefPrivateKey = null;    
					 Field[] wcefFields = WifiConfiguration.class.getFields();                     
					 // Dispatching Field vars                   
					 for (Field wcefField : wcefFields)                      
					 {                        
						 if (wcefField.getName().trim().equals(INT_ANONYMOUS_IDENTITY))          
							 wcefAnonymousId = wcefField;                      
						 else if (wcefField.getName().trim().equals(INT_CA_CERT))    
							 wcefCaCert = wcefField;                         
						 else if (wcefField.getName().trim().equals(INT_CLIENT_CERT))        
							 wcefClientCert = wcefField;                        
						 else if (wcefField.getName().trim().equals(INT_EAP))             
							 wcefEap = wcefField;                       
						 else if (wcefField.getName().trim().equals(INT_IDENTITY))                     
							 wcefIdentity = wcefField;                   
						 else if (wcefField.getName().trim().equals(INT_PASSWORD))                    
							 wcefPassword = wcefField;                     
						 else if (wcefField.getName().trim().equals(INT_PHASE2))                      
							 wcefPhase2 = wcefField;                    
						 else if (wcefField.getName().trim().equals(INT_PRIVATE_KEY))     
							 wcefPrivateKey = wcefField;                    
					 }            
					 Method wcefSetValue = null;     
					 if(!noEnterpriseFieldType)     
					 {                
						 for(Method m: wcEnterpriseField.getMethods())        
							 //System.out.println(m.getName());              
							 if(m.getName().trim().equals("value")){          
								 wcefSetValue = m;                    
								 break;                 
							 }               
					 }                  
					 /*EAP Method*/               
					 String result = null;            
					 Object obj = null;              
					 if(!noEnterpriseFieldType)            
					 {                    
						 obj = wcefSetValue.invoke(wcefEap.get(config), null);             
						 String retval = (String)obj;                     
						 Log.d("<<<<<<<<<<WifiPreference>>>>>>>>>>>>", "[EAP METHOD]" + retval);          
						 out.write("<<<<<<<<<<WifiPreference>>>>>>>>>>>>" + "[EAP METHOD]" + retval);      
					 }                    /*phase 2*/                
					 if(!noEnterpriseFieldType)     
					 {                    
						 result = (String) wcefSetValue.invoke(wcefPhase2.get(config), null);          
						 Log.d("<<<<<<<<<<WifiPreference>>>>>>>>>>>>", "[EAP PHASE 2 AUTHENTICATION]" + result);   
						 out.write("<<<<<<<<<<WifiPreference>>>>>>>>>>>>" + "[EAP PHASE 2 AUTHENTICATION]" + result);  
                     }                   
					 /*Anonymous Identity*/            
					 if(!noEnterpriseFieldType)                 
					 {                     
						 result = (String) wcefSetValue.invoke(wcefAnonymousId.get(config),null);                  
						 Log.d("<<<<<<<<<<WifiPreference>>>>>>>>>>>>", "[EAP ANONYMOUS IDENTITY]" + result);
						 out.write("<<<<<<<<<<WifiPreference>>>>>>>>>>>>" + "[EAP ANONYMOUS IDENTITY]" + result);      
					 }              
					 /*CA certificate*/               
					 if(!noEnterpriseFieldType)  
					 {                     
						 result = (String) wcefSetValue.invoke(wcefCaCert.get(config), null);      
						 Log.d("<<<<<<<<<<WifiPreference>>>>>>>>>>>>", "[EAP CA CERTIFICATE]" + result);                   
						 out.write("<<<<<<<<<<WifiPreference>>>>>>>>>>>>" + "[EAP CA CERTIFICATE]" + result);    
				     }                    
					 /*private key*/                 
					 if(!noEnterpriseFieldType)                 
					 {                     
						 result = (String) wcefSetValue.invoke(wcefPrivateKey.get(config),null);                 
						 Log.d("<<<<<<<<<<WifiPreference>>>>>>>>>>>>", "[EAP PRIVATE KEY]" + result);         
						 out.write("<<<<<<<<<<WifiPreference>>>>>>>>>>>>" + "[EAP PRIVATE KEY]" + result);     
					 }                   
					 /*Identity*/                
					 if(!noEnterpriseFieldType)              
					 {                      
						 result = (String) wcefSetValue.invoke(wcefIdentity.get(config), null);      
						 Log.d("<<<<<<<<<<WifiPreference>>>>>>>>>>>>", "[EAP IDENTITY]" + result);     
						 out.write("<<<<<<<<<<WifiPreference>>>>>>>>>>>>" + "[EAP IDENTITY]" + result);           
				     }                
					 /*Password*/                 
					 if(!noEnterpriseFieldType)                 
					 {                    
						 result = (String) wcefSetValue.invoke(wcefPassword.get(config), null);      
						 Log.d("<<<<<<<<<<WifiPreference>>>>>>>>>>>>", "[EAP PASSWORD]" + result);                   
						 out.write("<<<<<<<<<<WifiPreference>>>>>>>>>>>>" + "[EAP PASSWORD]" + result);              
					 }                    /*client certificate*/                 
					 if(!noEnterpriseFieldType)     
					 {                  
						 result = (String) wcefSetValue.invoke(wcefClientCert.get(config), null);         
						 Log.d("<<<<<<<<<<WifiPreference>>>>>>>>>>>>", "[EAP CLIENT CERT]" + result);             
						 out.write("<<<<<<<<<<WifiPreference>>>>>>>>>>>>" + "[EAP CLIENT CERT]" + result);          
						 Toast toast1 = Toast.makeText(context, "All config data logged to ReadConfigLog.txt", 3000);            
						 Toast toast2 = Toast.makeText(context, "Extract ReadConfigLog.txt from SD CARD", 5000);                 
					 }                    
					 out.close();                
				}catch(IOException e) {            
					Toast toast1 = Toast.makeText(context, "Failed to write Logs to ReadConfigLog.txt", 3000);        
					Toast toast2 = Toast.makeText(context, "Please take logs using Logcat", 5000);                 
					Log.e("<<<<<<<<<<WifiPreference>>>>>>>>>>>>", "Could not write to ReadConfigLog.txt" + e.getMessage());
				} catch(Exception e)                
				{                   
					e.printStackTrace();      
				}               
			}         
		}      
	}  


}
